package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.OnlinePayModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.OnlinePayContract;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.presenter.OnlinePayPresenter;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/10/22.
 * 线上支付服务费
 */

public class OnlinePayActivity extends BaseActivity<OnlinePayPresenter> implements OnlinePayContract.OnlinePayIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_payService)
    TextView tvPayService;
    @BindView(R.id.tv_alipay_msg)
    TextView aliPayMsg;
    @BindView(R.id.tv_wechat_msg)
    TextView weChatMsg;
    @BindView(R.id.rg_payment)
    RadioGroup radioGroup;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;


    private int orderID;
    private OnlinePayModel model;
    private int index;

    @Override
    protected int setLayout() {
        return R.layout.activity_onlinepay;
    }

    @Override
    protected OnlinePayPresenter bindPresenter() {
        return new OnlinePayPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("线上支付");
        Bundle extras = getIntent().getExtras();
        orderID = extras.getInt(Constant.ORDER_ID);
        index = extras.getInt(Constant.INDEX);
        mPresenter.getPayData(orderID);
    }

    @Override
    protected void initListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                tvSubmit.setText(model.getPayInfo(checkedId));
            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }

    @OnClick(R.id.tv_submit)
    void onSubmit() {
        mPresenter.submit(orderID, radioGroup.getCheckedRadioButtonId(), index);
    }

    @Override
    public void setPayData(OnlinePayModel data) {
        model = data;
        ivType.setImageDrawable(ContextCompat.getDrawable(this, OrderTypeEnum.getImg(model.getOrderType())));
        tvType.setText(OrderTypeEnum.getValue(model.getOrderType()));
        tvAmount.setText(data.getLastAmount());
        tvPayService.setText(data.getServiceFee());
        aliPayMsg.setText(data.getAliMsg());
        weChatMsg.setText(data.getWeMsg());
        tvSubmit.setText(data.getPayInfo(R.id.rbt_alipay));
    }


    @Subscribe(code = RxBusCode.WECHAT_PAY_SUCCESS)
    public void weChatPaySuccess() {
        RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.waitLoan.getStatus()));
        ToastUtil.showToast("支付成功");
        finish();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
